﻿using Basic.FlowApplication.Interface;
using Basic.FlowExtendService;
using Basic.FormRemoteModel;
using Basic.FormRemoteModel.QueryInter.Model;
using WeDonekRpc.Helper;

namespace Basic.FlowApplication.lmpl
{
    [WeDonekRpc.Client.Attr.ClassLifetimeAttr(WeDonekRpc.Client.Attr.ClassLifetimeType.SingleInstance)]
    internal class FormValFormatService : IFormValFormatService
    {
        private readonly IEmpService _Emp;
        private readonly IDictItemService _DictItem;
        private readonly IUnitDeptService _UnitDept;
        private readonly IThreeDicService _TreeDic;
        private readonly ICompanyService _Company;
        public FormValFormatService ( IEmpService emp,
            IDictItemService dictItem,
            IUnitDeptService unitDept,
            ICompanyService com,
            IThreeDicService treeDic )
        {
            this._Company = com;
            this._Emp = emp;
            this._DictItem = dictItem;
            this._UnitDept = unitDept;
            this._TreeDic = treeDic;
        }

        public void Format ( FormVal val )
        {
            if ( val.IsNull || val.FuncType == DataFuncType.基本 || val.FuncType == DataFuncType.选项值 || val.FuncType == DataFuncType.上传的附件 )
            {
                return;
            }
            else if ( val.DataType == FormRecordDataType.ListItem )
            {
                this._InitFormVal(val.ListItem, val);
            }
            else
            {
                this._InitFormVal(val);
            }
        }
        private void _InitFormVal ( FormListItem val, FormVal form )
        {
            if ( val.DataType == FormRecordDataType.Array )
            {
                List<string> ids = new List<string>();
                val.Vals.ForEach(c =>
                {
                    c.ForEach(a =>
                    {
                        ids.Add(a);
                    });
                });
                if ( form.FuncType == DataFuncType.人员ID )
                {
                    Dictionary<long, string> names = this._Emp.GetNames(ids.Distinct().Select(long.Parse).ToArray());
                    val.Vals = val.Vals.ConvertAll(a => a.ConvertAll(c => names[long.Parse(c)]));
                }
                else if ( form.FuncType == DataFuncType.部门单位ID )
                {
                    Dictionary<long, string> names = this._UnitDept.GetNames(ids.Distinct().Select(long.Parse).ToArray());
                    val.Vals = val.Vals.ConvertAll(a => a.ConvertAll(c => names[long.Parse(c)]));
                }
                else if ( form.FuncType == DataFuncType.公司ID )
                {
                    Dictionary<long, string> names = this._Company.GetNames(ids.Distinct().Select(long.Parse).ToArray());
                    val.Vals = val.Vals.ConvertAll(a => a.ConvertAll(c => names[long.Parse(c)]));
                }
                else if ( form.FuncType == DataFuncType.字典项 )
                {
                    Dictionary<string, string> names = this._DictItem.GetNames(form.DicId.Value, ids.Distinct().ToArray());
                    val.Vals = val.Vals.ConvertAll(a => a.ConvertAll(c => names[c]));
                }
                else
                {
                    Dictionary<string, string> names = this._TreeDic.GetNames(form.DicId.Value, ids.Distinct().ToArray());
                    val.Vals = val.Vals.ConvertAll(a => a.ConvertAll(c => names[c]));
                }
            }
            else if ( form.FuncType == DataFuncType.人员ID )
            {
                val.DataType = FormRecordDataType.String;
                long[] ids = val.LongVal.Where(a => a.HasValue).Select(a => a.Value).Distinct().ToArray();
                val.StrVal = this._Emp.GetName(ids);
            }
            else if ( form.FuncType == DataFuncType.部门单位ID )
            {
                val.DataType = FormRecordDataType.String;
                long[] ids = val.LongVal.Where(a => a.HasValue).Select(a => a.Value).Distinct().ToArray();
                val.StrVal = this._UnitDept.GetName(ids);
            }
            else if ( form.FuncType == DataFuncType.公司ID )
            {
                val.DataType = FormRecordDataType.String;
                long[] ids = val.LongVal.Where(a => a.HasValue).Select(a => a.Value).Distinct().ToArray();
                val.StrVal = this._Company.GetName(ids);
            }
            else if ( form.FuncType == DataFuncType.字典项 )
            {
                val.StrVal = this._DictItem.GetName(form.DicId.Value, val.StrVal);
            }
            else
            {
                val.StrVal = this._TreeDic.GetName(form.DicId.Value, val.StrVal);
            }
        }
        private void _InitFormVal ( FormVal val )
        {
            if ( val.DataType == FormRecordDataType.Array )
            {
                if ( val.FuncType == DataFuncType.人员ID )
                {
                    val.Vals = this._Emp.GetName(val.Vals.ConvertAll(long.Parse));
                }
                else if ( val.FuncType == DataFuncType.部门单位ID )
                {
                    val.Vals = this._UnitDept.GetName(val.Vals.ConvertAll(long.Parse));
                }
                else if ( val.FuncType == DataFuncType.公司ID )
                {
                    val.Vals = this._Company.GetName(val.Vals.ConvertAll(long.Parse));
                }
                else if ( val.FuncType == DataFuncType.字典项 )
                {
                    val.Vals = this._DictItem.GetName(val.DicId.Value, val.Vals);
                }
                else
                {
                    val.Vals = this._TreeDic.GetName(val.DicId.Value, val.Vals);
                }
            }
            else if ( val.FuncType == DataFuncType.人员ID )
            {
                val.DataType = FormRecordDataType.String;
                val.StrVal = this._Emp.GetName(val.LongVal.Value);
            }
            else if ( val.FuncType == DataFuncType.部门单位ID )
            {
                val.DataType = FormRecordDataType.String;
                val.StrVal = this._UnitDept.GetName(val.LongVal.Value);
            }
            else if ( val.FuncType == DataFuncType.公司ID )
            {
                val.DataType = FormRecordDataType.String;
                val.StrVal = this._Company.GetName(val.LongVal.Value);
            }
            else if ( val.FuncType == DataFuncType.字典项 )
            {
                val.StrVal = this._DictItem.GetName(val.DicId.Value, val.StrVal);
            }
            else
            {
                val.StrVal = this._TreeDic.GetName(val.DicId.Value, val.StrVal);
            }
        }
    }
}
